/*
 * Copyright (c) 2011 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

package com.google.api.client.generator.discovery;

import com.google.api.client.generator.common.AbstractFileGenerator;
import com.google.api.client.generator.common.Generation;
import com.google.api.client.generator.discovery.model.ClassModel;
import com.google.api.client.generator.discovery.model.ServiceModel;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson.JacksonFactory;
import com.google.api.services.discovery.Discovery;
import com.google.api.services.discovery.model.DirectoryList;
import com.google.api.services.discovery.model.DirectoryListItems;
import com.google.api.services.discovery.model.RestDescription;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Yaniv Inbar
 */
public class Generate {

  static final NetHttpTransport TRANSPORT = new NetHttpTransport();
  static final JsonFactory JSON_FACTORY = new JacksonFactory();
  static final Discovery DISCOVERY = new Discovery(TRANSPORT, JSON_FACTORY);

  public static void main(String[] args) throws IOException {
    System.out.println("Generator for the Google API Client Library for Java");
    if (args.length < 1) {
      System.err.println("Expected arguments: outputProjectDirectory");
      System.exit(1);
    }
    // compute file generators
    List<AbstractFileGenerator> fileGenerators = new ArrayList<AbstractFileGenerator>();
    DirectoryList directoryList = DISCOVERY.apis.list().execute();
    for (DirectoryListItems item : directoryList.getItems()) {
      RestDescription description =
          DISCOVERY.apis.getRest(item.getName(), item.getVersion()).execute();
      ServiceModel service = new ServiceModel(item, description);
      for (ClassModel classModel : service.getClassModels().values()) {
        fileGenerators.add(new ModelFileGenerator(classModel));
      }
    }
    Generation.compute(fileGenerators, Generation.getDirectory(args[0]));
  }
}
